﻿app.controller('ChargeCtrl',
    ['$scope', '$http', '$filter', 'httpPlus', '$stateParams', 'utils',
    function ($scope, $http, $filter, httpPlus, $stateParams, utils) {
        utils.setTitle('会员卡充值');

        var pageMgr = {
            pageNo: 1,
            pageSize: 6,
            total: 0
        }
        $scope.cardInfo = {
            cardNo: "-",
            cardValue: 0,
            cardGiftValue: 0,
            storeValueLink: undefined
        };
        $scope.btns = [
            {
                "name": "我要充值",
                "id": 1
            },
            {
                "name": "充值消费记录",
                "id": 2
            }
        ];
        $scope.currBtn = undefined;
        $scope.chargeAmount = [
            {
                "name": "100",
                "value": 100
            },
            {
                "name": "200",
                "value": 200
            },
            {
                "name": "300",
                "value": 300
            },
            {
                "name": "400",
                "value": 400
            }
        ];
        $scope.currCharge = $scope.chargeAmount[0];
        $scope.chargeDetails = [];
        $scope.monthChargeDetails = [];
        $scope.canShowMore = function () {
            return !(pageMgr.pageNo * pageMgr.pageSize >= pageMgr.total);
        };
        $scope.btnCurrBtn_click = function (data) {
            $scope.currBtn = data;
        }
        $scope.btnCurrCharge_click = function (data) {
            $scope.currCharge = data;
        };
        $scope.showMore_click = function () {
            if ($scope.canShowMore()) {
                pageMgr.pageNo++;
                refreshMemberItem(pageMgr.pageNo, pageMgr.pageSize);
            }

        }
        var refreshMemberItem = function (pageIndex, pageSize) {
            return httpPlus
                .RRRApi('MemberCenterPage/GetMemberItemListPage', {
                    _cardno: $scope.cardInfo.cardNo,
                    _pageno: pageIndex,
                    _pagesize: pageSize
                })
                .then(function (data) {
                    pageMgr.total = data.DataTotal;
                    data.DataList.forEach(function (val) {
                        $scope.chargeDetails.push({
                            itemTag: val.ItemTag,
                            billDateTime: val.BillDateTime.replace(/-/g, '/'),
                            cardValueCost: val.CardValueCost,
                            giftPoints: val.GiftPoint,
                            itemTagStr: val.ItemTagStr
                        })
                    });
                    $scope.monthChargeDetails = _.groupBy($scope.chargeDetails, function (_item) {
                        return $filter('date')(new Date(_item.billDateTime), "MM");
                    });
                });
        };
        httpPlus
            .RRRApi('MemberCenterPage/InitStoreValuePage', {
                _cardid: $stateParams.CardID,
                _absoluteUrl: window.location.href
            })
            .then(function (data) {
                if ($stateParams.Type == 1)
                    $scope.currBtn = $scope.btns[1];
                else
                    $scope.currBtn = $scope.btns[0];
                $scope.cardInfo.cardNo = data.CardNo;
                $scope.cardInfo.cardValue = data.CardValue;
                $scope.cardInfo.cardGiftValue = data.CardGiftValue;
                $scope.cardInfo.storeValueLink = data.StoreValueLink;
            })
            .then(function () {
                return refreshMemberItem(pageMgr.pageNo, pageMgr.pageSize);
            }, function (error) {
                console.log(error);
            })

    }]);